<?php

namespace App\Service;

use App\Exceptions\OrException;
use App\Models\WithDraw;
use App\Models\AgentsComm;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class DrawService
{
	/**
	 * 提现审核
	 * @param array $data
	 * @return bool
	 * @throws OrException
	 */
    public function UpdataStatus(array $data, $id)
    {		
		$models = WithDraw::where('id', $id)->first(); 
		if($models->status != 0) {
			return ['status' => -1, 'error' => '该记录已经审核过，请不要重复审核'];
		}
        $models->order_id = $data['order_id'];
		$models->status = $data['status'];
		$models->updated_at = date('Y-m-d H:i:s');
		$guserccount = $this->AccountComm($data, $models);
        if ($models->save() && $guserccount) {
            return ['status' => 1, 'error' =>'成功'];
        }
        return ['status' => 0, 'error' =>'失败'];
    }
	/**
	 * 资金处理
	 * @param type $data
	 * @param type $withdraw
	 */
	public function AccountComm($data, $withdraw){
		$gcomm = \App\Models\GuildUserComm::where(['gid' => $withdraw->gid, 'guser_id' => $withdraw->guser_id])->first();
		if($data['status'] == 1){			
			$gcomm->total_draw_money += $withdraw->money;	//累计提现佣金金额
		}
		if($data['status'] == 2) {
			$gcomm->balance += $withdraw->money;	//失败退回账户
		}
		if($gcomm->save()) {
			return true;
		}
		return false;
	}
	/**
	 * 支付宝绑定审核
	 * @param array $data
	 * @return bool
	 * @throws OrException
	 */
    public function UpdataAlipayStatus(array $data, $id)
    {		
		$models = \App\Models\WithdrawAccount::where('id', $id)->first();
		if($models->status != 0) {
			return ['status' => -1, 'error' => '该记录已经审核过，请不要重复审核'];
		}
		$models->status = $data['status'];
		$models->updated_at = date('Y-m-d H:i:s');		
        if ($models->save()) {
            return ['status' => 1, 'error' =>'成功'];
        }
        return ['status' => 0, 'error' =>'失败'];
	}
	/**
	 * 提现账户信息添加
	 */
	public function addDrawaAccount($data, $gid, $guser_id) {
		$validator = Validator::make($data, [
			'mobile' => 'regex:/^1[3-9]\d{9}$/|unique:withdraw_account,mobile',
			'alipay' => ['regex:/^[1-9]\d{4,10}$|(\w[-\w.+]*@(qq|gmail|163|126|139|sohu|aliyun|189)\.(com|cn)(\r\n|\r|\n)?$)/', 'unique:withdraw_account,alipay'],	
			'name' => 'regex:/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/|unique:withdraw_account,name',			
			'alipay_code' => 'required',			
        ],[
			'mobile.regex' => '手机号不正确',
			'mobile.unique' => '手机号已使用',
			'alipay.unique'	=> '支付宝账号已经存在',
			'alipay.regex'	=> '支付宝账号输入错误',		
			'name.regex' => '支付宝姓名输入必须是中文',	
			'name.unique' => '支付宝姓名已使用',							
			'alipay_code.required' =>'请输入支付宝收款码',
        ]);
        $error = $validator->errors()->first();
		if ($error) {
			return ['status' => -1, 'error' => $error];
		}
		$data['created_at'] = date('Y-m-d H:i:s');
		$data['gid'] = $gid;
		$data['guser_id'] = $guser_id;
		$res = new \App\Models\WithdrawAccount($data);
        if ($res->save()) {
            return ['status' => 1, 'error' =>'成功'];
        }
        return ['status' => 0, 'error' =>'失败'];
	}
	/**
	 * 提现账户信息审核
	 * $type 默认总后台修改 1公会提交  
	 */
	public function upDrawaAccount($data, $withdrawAccountModels, $id, $type=null) {
		$validator = Validator::make($data, [
			'name' => ['regex:/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/', Rule::unique('withdraw_account', 'name')->ignore($id)],
			'alipay' => ['regex:/^[1-9]\d{4,10}$|(\w[-\w.+]*@(qq|gmail|163|126|139|sohu|aliyun|189)\.(com|cn)(\r\n|\r|\n)?$)/', Rule::unique('withdraw_account', 'alipay')->ignore($id)],
            'mobile' => ['regex:/^1[3-9]\d{9}$/', Rule::unique('guild_user', 'mobile')->ignore($id)],			
			'alipay_code' => 'required',	
			
        ],[
			'name.regex' => '支付宝姓名输入必须是中文',	
			'name.unique' => '支付宝姓名已使用',
			'alipay.unique'	=> '支付宝账号已经存在',
			'alipay.regex'	=> '支付宝账号输入错误',	
			'mobile.regex' => '手机号不正确',
			'mobile.unique' => '手机号已使用',			
			'alipay_code.required' =>'请支付宝收款码',
        ]);
        $error = $validator->errors()->first();
		if ($error) {
			return ['status' => -1, 'error' => $error];
		}
		if($type === 1) {
			$data['status'] = 0;
		}
		$data['updated_at'] = date('Y-m-d H:i:s');
		$res = $withdrawAccountModels->where('id', $id)->update($data);
        if ($res) {
            return ['status' => 1, 'error' =>'成功'];
        }
        return ['status' => 0, 'error' =>'失败'];
	}

	/**
	 * 账号日志流水
	 */
	public function commlog() {
		
	}
	
	
}